package com.artist.controller;

import com.artist.entity.TbUser;
import com.artist.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "用户模块管理")
@Controller
public class UserController {
    @Autowired
    private UserService    userService;
    @ApiOperation(value = "用户列表")
    @GetMapping("/user")
    public String selectAllUser(HttpServletRequest request, Model model){
        List<TbUser> list = userService.selectAll();
        model.addAttribute(list);
        request.setAttribute("list",list);
        return "alluser";
    }
    @ApiOperation(value = "查询用户")
    @GetMapping("/user/{uid}")
    @ResponseBody
    public TbUser selectTbUserByPrimaryKey(@PathVariable Integer uid){
        TbUser user = userService.selectUserPrimaryKey(uid);
        return user;
    }
    @ApiOperation(value = "增加用户")
    @PostMapping("/user")
    public void insertTbUser(@RequestParam Integer tId,@RequestParam String uName, @RequestParam String uPassword){
        TbUser tbUser = new TbUser();
        tbUser.setTId(tId);
        tbUser.setUName(uName);
        tbUser.setUPassword(uPassword);
         userService.savaUser(tbUser);
    }
    @ApiOperation(value = "编辑用户")
    @PutMapping("/user/{uid}")
    @ResponseBody
    public  String updateUserByPrimaryKey(@RequestBody TbUser tbUser){
        userService.updateUserPrimaryKeySelective(tbUser);
        return "user";
    }
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/user/{uid}")
    public int deleteTbUser(@PathVariable Integer uid){
        userService.deleteUserPrimaryKey(uid);
        return 0;
    }

}
